<template>
    <div class="login">
        <form>
            <div class="login-header">登录</div>
            <div class="form-group">
                <label>账号</label>
                <input type="text" class="form-control" v-model="name" placeholder="请输入账号">
            </div>
            <div class="form-group">
                <label>密码</label>
                <input type="password" class="form-control" v-model="password" placeholder="请输入密码">
            </div>
            <button type="submit" class="btn">登录</button>
            <!--<router-link :to="{name:'register'}"><span>没有账号？马上注册</span></router-link>-->
            <a class="version_status" :class="{
                 'latest_version':versionCode === 1,
                 'big_update':versionCode === 2 || versionCode === 0,
                 'small_update':versionCode === 3,
                 'version_check':![1,2,3].includes(versionCode),
                 'topLevel':isTopLevel && showVersionTips,
             }" @click.prevent="userUpdate">
                <span class="version_status_tips">{{tips || versionStatus}}</span>
                <progress :value="downloadPercent" max="100" v-show="isTopLevel && showVersionTips && downloadPercent"></progress>
                <span v-show="isTopLevel && showVersionTips && downloadPercent">{{downloadPercent | float2percent}}</span>
            </a>
        </form>
        <!-- <version-tips v-if="showVersionTips" :versionTips="versionTips" :appVersion="appVersion" :canCancel="canCancel" @updateNow="updateNow" @userCancel="showVersionTips = false"></version-tips> -->
    </div>
</template>

<style scoped lang="less">
.login {
    position: fixed;
    width: 100%;
    height: 100%;
    z-index: 99;
}

.login-header {
    width: 100%;
    line-height: 50px;
    text-align: center;
    background: #f5f5f5;
    margin-bottom: 20px;
    font-size: 16px;
    border-radius: 5px;
}

.login form {
    width: 300px;
    height: 340px;
    border-radius: 5px;
    border: 1px solid #bbb;
    position: absolute;
    left: calc(~"50% - 150px");
    top: calc(~"50% - 170px");
}

.form-group {
    padding: 0 20px;
    display: flex;
    flex-direction: column;
}

.form-control {
    margin: 10px 0;
    padding: 5px;
    outline: 0;
}

label {
    display: flex;
}

button {
    width: 258px;
    padding: 6px 0;
    margin: 10px 20px;
    background: #428bca;
    outline: none;
    font-size: 16px;
    cursor: pointer;
    border: none;
    border-radius: 3px;
}

.version_status {
    text-align: center;
    display: block;
    margin: 20px;
    color: #1d90e6;
    &.latest_version {
        color: green;
    }
    &.small_update {
        color: orange;
        cursor: pointer;
    }
    &.big_update {
        color: red;
        font-weight: bold;
    }

    &.topLevel {
        position: fixed;
        top: 10px;
        left: 0;
        width: 100%;
        z-index: 999;
    }
    .version_status_tips {
        display: block;
    }
}
</style>

<script>
import { api, checkVersion } from "@/js/utils";
import { ipcRenderer } from "electron";
// import versionTips from "./ListPopup/VersionTips.vue";
const appVersion = require("../../../package.json").version;
export default {
    name: "landing-page",
    data: function() {
        return {
            name: "",
            password: "",
            versionCode: "0",
            tips: "",
            showVersionTips: false,
            versionTips: "",
            appVersion: "",
            canCancel: false,
            isTopLevel: false,
            downloadPercent: 0,
        };
    },
    filters: {
        float2percent(n) {
            return n.toFixed(2) + "%";
        },
    },
    components: {
        // "version-tips": versionTips,
    },
    computed: {
        versionStatus: function() {
            switch (this.versionCode) {
                case 0:
                    return "服务器版本错误，请联系开发者";
                case 1:
                    return "最新版本，无需更新！";
                case 2:
                    return "版本即将强制升级！";
                case 3:
                    return "存在新版本，点击立即更新！";
                default:
                    return "版本检查中";
            }
        },
    },
    created() {
        //设置窗口title为当前版本
        document.title = `${document.title}-v${appVersion}`;

        ipcRenderer.on("message", (event, text) => {
            console.log(arguments);
            console.warn(event, text, appVersion);
            const latestVersion = text.version;
            console.warn(text);
            console.log(parseFloat(appVersion));
            this.versionCode = checkVersion(appVersion, latestVersion);
            // this.tips = text;
        });
        ipcRenderer.on("downloadProgress", (event, progressObj) => {
            console.log(progressObj);
            this.downloadPercent = progressObj.percent || 0;
        });
        ipcRenderer.on("isUpdateNow", () => {
            ipcRenderer.send("isUpdateNow");
        });
        ipcRenderer.send("checkForUpdate");
    },
    beforeDestroy() {
        //组件销毁前移除所有事件监听channel
        ipcRenderer.removeAllListeners([
            "message",
            "downloadProgress",
            "isUpdateNow",
        ]);
    },
    methods: {
        userUpdate() {
            if (3 === this.versionCode) {
                console.log("downloadUpdate");
                ipcRenderer.send("downloadUpdate");
            }
        },
    },
};
</script>